<template>
  <div>
    <h1>登录</h1>
    <el-rate
      v-model="value"
      show-text>
    </el-rate>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="手机号码" prop="tel">
          <el-input type="text" v-model="ruleForm.tel" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import { Form, FormItem, Input, Button, Notification, Rate } from 'element-ui'
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Button)
Vue.use(Rate)
Vue.use(Notification)
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) { // 正则代替
        callback(new Error('密码长度不能少于6位'))
      } else {
        callback()
      }
    }
    var validateTel = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (value.length !== 11) { // 正则代替
        callback(new Error('请输入正确格式的手机号码'))
      } else {
        callback()
      }
    }
    return {
      value: 3,
      ruleForm: {
        tel: '18813007814',
        password: '123456'
      },
      rules: {
        tel: [
          { validator: validateTel, trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      console.log(formName)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    login () {
      axios.post('http://localhost:3000/api/users/login', {
        tel: this.ruleForm.tel,
        password: this.ruleForm.password
      }).then(res => {
        console.log(res.data)
        const { code } = res.data
        switch (code) {
          case '10000':
            this.$notify.error({
              title: '错误',
              message: '信息填写不完整'
            })
            break
          case '10808':
            this.$notify({
              title: '警告',
              message: '该用户未注册',
              type: 'warning'
            })
            break
          case '10101':
            this.$notify({
              title: '警告',
              message: '密码错误',
              type: 'warning'
            })
            break
          case '10888':
            this.$notify({
              title: '成功',
              message: '登录',
              type: 'success'
            })
            // 保存用户的信息，返回上一页
            break
          default:
            break
        }
      })
    }
  }
}
</script>
